const gulp = require('gulp');
const bable = require('gulp-babel');
const watch = require('gulp-watch');
const rollup = require('gulp-rollup'); //tree-shking
const replace = require('rollup-plugin-replace');
const gulpSequence = require('gulp-sequence'); //执行队列

gulp.task('builddev', () => {
  return watch('./src/nodeuii/**/*.js', {
    ignoreInitial: false
  }, () => {
    gulp.src('./src/nodeuii/**/*.js')
      .pipe(
        bable({
          babelrc: false,
          plugins: "transform-es2015-modules-commonjs"
        })
      )
      .pipe(gulp.dest('dist'));
  })
})

gulp.task('buildprod', () => {

  gulp.src('./src/nodeuii/**/*.js')
    .pipe(
      bable({
        babelrc: false,
        ignore: './src/nodeuii/config/*.js',
        plugins: "transform-es2015-modules-commonjs"
      })
    )
    .pipe(gulp.dest('dist'));
})

gulp.task('buildconfig', () => {
  gulp.src('./src/nodeuii/**/*.js')
    .pipe(rollup({
      output: {
        format: 'cjs'
      },
      input: './src/nodeuii/config/index.js',
      plugins: [
        replace({
          'process.env.NODE_ENV': JSON.stringify('production')
        })
      ]
    }))
    .pipe(gulp.dest('./dist'));
})

let _task = ["builddev"];
if(process.env.NODE_ENV=='production'){
  console.log('process.env.NODE_ENV',process.env.NODE_ENV);
   _task = gulpSequence("buildprod","buildconfig")
}

gulp.task("default", _task);
